// 请求封装
const BASE_URL = "http://192.168.1.107:50000";

// Authorization: "Bearer " + token
// X-Authorization: "Bearer " + token

// 封装请求函数
const request = ({ url, data, method = "GET" }: any) => {
  let header = {};

  // if (token) {
  // 	header["Authorization"] = "Bearer " + token
  // } else {
  // 	token = {}
  // }
  console.log("请求接口地址：", BASE_URL + url);
  console.log("请求接口参数：", data);
  return wx
    .request({
      url: BASE_URL + url,
      data,
      method,
      header,
    })
    .then((res: any) => {
      const [err, data] = res;
      return successHandler(data.data);
    })
    .catch((err) => {
      return failHandler(err);
    })
    .finally(() => {
      hideToast();
    });
};

// 请求成功
function successHandler(response:any) {
  const { code, data, message } = response;
  if (code === 200) {
    return Promise.resolve(data);
  }
  if (code === 401) {
    // clearStorage();
    // replace("/pages/login/login")
  }
  if (code === 500) {
    // showToast(message, "none", 2000);
  }
}

// 请求失败
function failHandler(err:any) {
  // showToast(err);
  return Promise.reject(err);
}

export default request;
